<?php
if (!defined('MVC_ROOT_DIR'))  die('Hacking attempt');
class Basic
{
	public static function getClientIp(){
		$ip=false;
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
            for ($i = 0; $i < count($ips); $i++) {
                if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
                    $ip = $ips[$i];
                    break;
                }
            }
        }
        $cip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
        return $cip;
	}
	
	public  static function gbkSubstr($str, $start, $len) { 
		$str_tmp = $len - $start; 
		if (strlen($str) < $str_tmp) { 
			$tmpstr = $str; 
		} else { 
			$tmpstr = ""; 
			$strlen = $start + $len; 
			for($i = 0; $i < $strlen; $i++) { 
				if(ord(substr($str, $i, 1)) > 0xa0) { 
					$tmpstr .= substr($str, $i, 2); $i++; 
				} else { 
					$tmpstr .= substr($str, $i, 1); 
				} 
			} $tmpstr .= ".."; 
		} 
		return $tmpstr; 
	}
	public static function  utfSubstr($str, $start, $length)
	{
		if (function_exists('mb_substr')) {
			$len = floor(mb_strlen($str)/3);			
			if ($len>$length){
				return mb_substr($str, $start, $length, 'UTF-8').'...';
			}else{
				return mb_substr($str, $start, $length, 'UTF-8');
			}
		}
		preg_match_all("/./u", $str, $arr);
		return implode("", array_slice($arr[0], $start, $length));		
	}
	
	/*
     * 中文截取，支持gb2312,gbk,utf-8,big5 
     *
     * @param string $str 要截取的字串
     * @param int $start 截取起始位置
     * @param int $length 截取长度
     * @param string $charset utf-8|gb2312|gbk|big5 编码
     * @param $suffix 是否加尾缀
     */
    public static function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
    {
        if(function_exists("mb_substr"))
        {
            if(mb_strlen($str, $charset) <= $length) return $str;
            $slice = mb_substr($str, $start, $length, $charset);
        }
        else
        {
            $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
            $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
            $re['gbk']          = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
            $re['big5']          = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
            preg_match_all($re[$charset], $str, $match);
            if(count($match[0]) <= $length) return $str;
            $slice = join("",array_slice($match[0], $start, $length));
        }
        if($suffix) return $slice."…";
        return $slice;
    }
	public static function dealPost($post){		
		foreach ($post as $k=>$v){
			if ($v==''){				
				unset($post[$k]);
			}else{
				trim($post[$k]);
			}
		}
		return $post;
	}
	public static function dealGet($get){
		foreach ($get as $k=>$v){
			if ($v==''){				
				unset($get[$k]);
			}else{
				trim($get[$k]);
			}
		}
		return $get;
	}
	
	public static function array2Json($array) { 
	    self::arrayRecursive($array, 'urlencode', true); 
	    $json = json_encode($array); 
	    $json = urldecode($json); 
	    // ext需要不带引号的bool类型 
	    $json = str_replace("\"false\"","false",$json); 
	    $json = str_replace("\"true\"","true",$json); 
	    return $json; 
	} 
	public static  function json2Array($json){	
		$json = str_replace("\r\n", "[ad0xx]", $json);	
		$json = str_replace("\r", "[x0d]", $json);
		$json = str_replace("\n", "[x0a]", $json);	
		
		$rs = json_decode($json,true);
		$rs = str_replace('[ad0xx]',"\n",$rs);
		$rs = str_replace('[x0d]',"\n",$rs);
		$rs = str_replace('[x0a]',"\n",$rs);
		return $rs;
	}
	static function  arrayRecursive(&$array, $function, $apply_to_keys_also = false) 
	{ 
	    static $recursive_counter = 0; 
	    if (++$recursive_counter > 1000) { 
	        die('possible deep recursion attack'); 
	    } 
	    foreach ($array as $key => $value) { 
	        if (is_array($value)) { 
	            self::arrayRecursive($array[$key], $function, $apply_to_keys_also); 
	        } else { 
	            $array[$key] = $function($value); 
	        } 
	
	        if ($apply_to_keys_also && is_string($key)) { 
	            $new_key = $function($key); 
	            if ($new_key != $key) { 
	                $array[$new_key] = $array[$key]; 
	                unset($array[$key]); 
	            } 
	        } 
	    } 
	    $recursive_counter--; 
	} 
	
	
	
	
}